package somar;

import java.awt.Button;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Hashtable;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.osgi.framework.BundleContext;
import org.xml.sax.XMLReader;

import somar_test_hello.parser.rselV1_4a.ListVisitor;
import somar_test_hello.parser.rselV1_4a.RSELSaxEvent;

public class AWTUI extends Frame implements WindowListener, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private BundleContext context;
	
	private Button btn;
	private Frame f;
	public AWTUI(BundleContext context) {
		this.context = context;
		f = this;
		setBounds(100, 100, 1000, 1000);
		
		btn = new Button();
		btn.setBounds(1, 1, 5, 5);
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				FileDialog fDialog = new FileDialog(f, "", FileDialog.LOAD);
				fDialog.setFilenameFilter(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						return name.endsWith(".rsel");
					}
				});
				
				fDialog.setVisible(true);
				String selectedRSELFile = fDialog.getDirectory() + fDialog.getFile();
				if(fDialog.getDirectory() == null && fDialog.getFile() == null) {
					System.out.println("[SOMAR Server] RSEL file is not selected.");
				} else {
					System.out.println("[SOMAR Server] RSEL file is selected. : " + selectedRSELFile);
					checkRSEL(selectedRSELFile);
				}
			}
			
		});
		
		this.add(btn);
		setVisible(true);
	}
	
	
	public void checkRSEL(String rsel) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			
			RSELSaxEvent se = new RSELSaxEvent();
			reader.setContentHandler(se);
			reader.parse(rsel);
			
			ListVisitor lv = new ListVisitor();
			se.getRootElement().accept(lv);

			System.out.println("[SOMAR Server] RSEL file is read.");
			System.out.println("[SOMAR Server] ComposedServiceName is : " + lv.getComposedSerivceName());
			
			registerComposedRobotService(lv.getComposedSerivceName(), rsel);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void registerComposedRobotService(String composedServiceName, String rsel) {
	
		Hashtable<String, String> props = new Hashtable<String, String>();
		props.put("SOAP.service.name", composedServiceName);
		
		ServerApplicationInterface composedServiceObject = new ServerApplicationInterfaceImpl();
		composedServiceObject.setRSEL(rsel);
		composedServiceObject.setComposedServiceName(composedServiceName);
		
		this.context.registerService(ServerApplicationInterfaceImpl.class.getName(), composedServiceObject, props);
		
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}